package com.sam.designpatterns

// 租客
class Tenant(var mediator: Mediator) {
    fun renting() {
        println("租客去找中介租房子")
        mediator.renting()
    }

    fun pay() {
        println("租客付房租给中介")
    }
}

// 房东
class Landlord(var mediator: Mediator) {
    fun collection() {
        println("房东去找中介要房租")
        mediator.collection()
    }
    fun rentOut() {
        println("房东通过中介把房子租出去了")
    }
}

class Mediator {
    var landlord: Landlord? = null
    var tenant: Tenant? = null

    fun renting() {
        landlord?.rentOut()?: println("没有房东出租房子，要睡大街了~")
    }

    fun collection() {
        tenant?.pay() ?: print("房子还未出租出去，没钱收哦~")
    }
}

fun main() {
    // 先要有个中介
    val mediator = Mediator()
    // 来了个租客找中介
    val tenant = Tenant(mediator)
    mediator.tenant = tenant
    tenant.renting() // 还没有房东的时候租房

    val landlord = Landlord(mediator)
    mediator.landlord = landlord
    tenant.renting() // 有了房东再租房
    landlord.collection()

    println("\n租客退租啦！")
    mediator.tenant = null
    landlord.collection()
}